package com.example.demospringmybatisplus.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demospringmybatisplus.entity.SecureInvokeRecord;
import com.example.demospringmybatisplus.mapper.SecureInvokeRecordMapper;
import com.example.demospringmybatisplus.service.SecureInvokeService;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

/**
 * SecureInvokeRecordDao
 *
 * @author hauhua
 * @DATE 2025/1/21
 */
@Component
public class SecureInvokeRecordDao extends ServiceImpl<SecureInvokeRecordMapper, SecureInvokeRecord> {

    public List<SecureInvokeRecord> getWaitRetryRecords() {
        Date now = new Date();
        // 查2分钟前的待处理数据。避免刚入库的数据被查出来。其中重试次数>3的状态是Fail的也是查询不出来的
        // select * from secure_invoke_record where retry_times < 3 and status = 1 and next_retry_time < now() and create_time < now() - interval '2 minute'
        DateTime afterTime = DateUtil.offsetMinute(now, 1);
        return lambdaQuery()
                .eq(SecureInvokeRecord::getStatus, SecureInvokeRecord.STATUS_WAIT)
                .lt(SecureInvokeRecord::getNextRetryTime, new Date())
                .lt(SecureInvokeRecord::getCreateTime, afterTime)
                .list();
    }
}
